package com.zhanghu.backendZray.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhanghu.backendZray.cache.ContextCache;
import com.zhanghu.backendZray.constants.CommonConstant;
import com.zhanghu.backendZray.pojo.entity.Member;
import com.zhanghu.backendZray.mapper.MemberMapper;
import com.zhanghu.backendZray.pojo.vo.MemberInfoVo;
import com.zhanghu.backendZray.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zhangdong
 * @since 2024-08-09
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Override
    public MemberInfoVo currentUserMemberInfo() {
        Integer currentUserId = ContextCache.currentUserId();
        Member member = getOne(new LambdaQueryWrapper<Member>().eq(Member::getUserId, currentUserId));
        if (member == null) {
            return new MemberInfoVo(false, CommonConstant.MEMBER_PRICE, LocalDate.now().plusYears(1));
        }
        LocalDate expiryDateEnd = member.getExpiryDateEnd();
        boolean expiry = expiryDateEnd.isBefore(LocalDate.now());
        if (expiry) {
            return new MemberInfoVo(false, CommonConstant.MEMBER_PRICE, LocalDate.now().plusYears(1));
        }
        return new MemberInfoVo(true, CommonConstant.MEMBER_PRICE, expiryDateEnd);
    }
}
